package code.starivan.leetcode;

/**
 * Created by Ivan on 2015/10/18.
 */
public class N006_ZigZagConversion {
    public static String convert(String s, int numRows) {
        if(s==null||s.length()==0||numRows<=1){
            return s;
        }

        char[] ret=new char[s.length()];
        int k=0;
        int step=2*numRows-2;
        for(int i=0;i<s.length();i+=step){
            ret[k++]=s.charAt(i);
        }

        for(int r=1;r<numRows-1;++r){
            for(int i=r;i<s.length();i+=step){
                ret[k++]=s.charAt(i);
                int next=i+2*(numRows-r)-2;
                if(next<s.length()){
                    ret[k++]=s.charAt(next);
                }
            }
        }

        for(int i=numRows-1;i<s.length();i+=step){
            ret[k++]=s.charAt(i);
        }

        return String.copyValueOf(ret);
    }
}
